package com.xpw.viewdemo.view

import android.content.Context
import android.graphics.*
import android.os.Build
import android.util.AttributeSet
import android.view.View
//val RADIUS = 100f.px  放在这里报错 因为在其他类已经定义了
val WIDTH=2f.px
val HEIGHT=10f.px
class DashboardView(context: Context?, attrs: AttributeSet?) : View(context, attrs) {
    val RADIUS = 100f.px
    private val dash=Path()
    private val path=Path()
    private val paint = Paint(Paint.ANTI_ALIAS_FLAG)//抗锯齿
    private lateinit var pathDashPathEffect: PathDashPathEffect
    init {
        paint.strokeWidth=3f.px
        paint.style=Paint.Style.STROKE
        dash.addRect(0f,0f,WIDTH,HEIGHT,Path.Direction.CCW)

    }
    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        path.reset()
        path.addArc(width/2f-150f.px,height/2-150f.px,width/2+150f.px,
            height/2+150f.px,150f,240f)
        val pathMeasure=PathMeasure(path,false)
        pathDashPathEffect=PathDashPathEffect(dash,pathMeasure.length/20,0f,PathDashPathEffect.Style.ROTATE)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            //画弧
            canvas.drawPath(path,paint)
            //画刻度
            paint.setPathEffect(pathDashPathEffect)
            canvas.drawArc(width/2f-150f.px,height/2-150f.px,width/2+150f.px,
                height/2+150f.px,150f,240f,false,paint)
            paint.setPathEffect(null)
        }
    }
}